package com.capgemini.remind.controller;

import com.capgemini.core.AjaxResponse;
import com.capgemini.core.PageList;
import com.capgemini.interceptor.DealerNumUtils;
import com.capgemini.remind.pojo.CustomerPromotion;
import com.capgemini.remind.service.CustomerPromotionService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/promotion/customer")
public class CustomerPromotionController {
	// 日志记录器
	public final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private CustomerPromotionService customerPromotionService;

	@RequestMapping("/manage/{dealerNum}")
	public Object manage(@PathVariable("dealerNum") String dealerNum1) {
		String dealerNum = DealerNumUtils.getDealerNum();

		ModelAndView mv = new ModelAndView("/remind/promotionCustomer.jsp");

		mv.addObject("dealerNum", dealerNum);

		return mv;
	}

	@RequestMapping("/list")
	@ResponseBody
	public Object list(PageList<CustomerPromotion> pageList, CustomerPromotion pojo) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		pageList.setParam(pojo);
		customerPromotionService.queryPage(pageList);

		ajaxResponse.setData(pageList);

		return ajaxResponse;
	}
	@RequestMapping("/load/{id}")
	@ResponseBody
	public Object loadById(@PathVariable("id") String id) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		ajaxResponse.setData(customerPromotionService.getById(id));

		return ajaxResponse;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(CustomerPromotion pojo) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		if(pojo.getPromotionalContent().length() > 50){

			ajaxResponse.setSuccess(false);
			ajaxResponse.setErrorCode("PARAM-ERROR");
			ajaxResponse.setMessage("模板内容过长（不能超过50字），请修改");

			return ajaxResponse;
		}

		if(pojo.getId() == null){
			customerPromotionService.insert(pojo);
		}else{
			customerPromotionService.updateById(pojo);
		}

		return ajaxResponse;
	}

	@RequestMapping("/list/{dealerId}")
	@ResponseBody
	public Object queryByTypeId(CustomerPromotion param){

		AjaxResponse ajaxResponse = new AjaxResponse();

		ajaxResponse.setData(customerPromotionService.queryList(param));

		return ajaxResponse;
	}


	@RequestMapping("/del/{id}")
	@ResponseBody
	public Object deleteById(@PathVariable("id") String id) {
		AjaxResponse ajaxResponse = new AjaxResponse();


		if(StringUtils.isBlank(id)){
			ajaxResponse.setSuccess(false);
			ajaxResponse.setErrorCode("PARAM-LOSS");
			ajaxResponse.setMessage("参数丢失");
		}else{
			customerPromotionService.deletePhysicalById(id);
		}

		return ajaxResponse;
	}

}
